package com.ruoyi.subject.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.subject.mapper.SubjectJudgeMapper;
import com.ruoyi.subject.mapper.SubjectTaskMapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.subject.domain.SubjectJudge;
import com.ruoyi.subject.domain.SubjectTask;
import com.ruoyi.subject.service.ISubjectJudgeService;

/**
 * 课题评审Service业务层处理
 * 
 * @author yepanpan
 * @date 2021-03-15
 */
@Service
public class SubjectJudgeServiceImpl implements ISubjectJudgeService 
{
    @Autowired
    private SubjectJudgeMapper subjectJudgeMapper;
    @Autowired
    private SubjectTaskMapper subjectTaskMapper;

    /**
     * 查询课题评审
     * 
     * @param id 课题评审ID
     * @return 课题评审
     */
    @Override
    public SubjectJudge selectSubjectJudgeById(Long id)
    {
        return subjectJudgeMapper.selectSubjectJudgeById(id);
    }

    /**
     * 查询课题评审列表
     * 
     * @param subjectJudge 课题评审
     * @return 课题评审
     */
    @Override
    public List<SubjectJudge> selectSubjectJudgeList(SubjectJudge subjectJudge)
    {
        return subjectJudgeMapper.selectSubjectJudgeList(subjectJudge);
    }

    /**
     * 新增课题评审
     * 
     * @param subjectJudge 课题评审
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSubjectJudge(SubjectJudge subjectJudge)
    {
    	if(subjectJudge.getTaskId() == null) {
    		throw new CustomException("请求参数有误");
    	}
    	
    	SubjectTask task = subjectTaskMapper.selectSubjectTaskById(subjectJudge.getTaskId());
    	if(task == null || "9".equals(task.getProgress())) {
    		throw new CustomException("课题不存在或者已经结题");
    	}
    	
    	if(Integer.parseInt(task.getProgress()) > Integer.parseInt(subjectJudge.getType())){
    		throw new CustomException("该阶段已经通过，不需要再次评审");
    	}
    	    	
    	int count = 1;
    	for(Long userId:subjectJudge.getUserIds()) {
    		SubjectJudge d = new SubjectJudge();
    		d.setType(subjectJudge.getType());
    		d.setCheckUserId(userId);
    		d.setTaskId(task.getId());
    		List<SubjectJudge> l = subjectJudgeMapper.selectSubjectJudgeList(d);
    		if(l == null || l.size() == 0) {
    			d.setCheckStatus("0");
    			subjectJudgeMapper.insertSubjectJudge(d);
    			count ++;
    		}
    	}
    	
        return count ++ ;
    }

    /**
     * 修改课题评审
     * 
     * @param subjectJudge 课题评审
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSubjectJudge(SubjectJudge subjectJudge)
    {    	
    	subjectJudge.setCheckTime(DateUtils.getNowDate());
        subjectJudgeMapper.updateSubjectJudge(subjectJudge);
        
        //判断是否全部评审通过
    	if("1".equals(subjectJudge.getCheckStatus())) {
    		boolean pass = true;
    		SubjectJudge d = new SubjectJudge();
    		d.setType(subjectJudge.getType());
    		d.setTaskId(subjectJudge.getTaskId());
    		List<SubjectJudge> l = subjectJudgeMapper.selectSubjectJudgeList(d);
    		for(SubjectJudge j:l) {
    			if(!"1".equals(j.getCheckStatus())) {
    				pass = false;
    				break;
    			}
    		}
    		if(pass) {
    			SubjectTask task = subjectTaskMapper.selectSubjectTaskById(subjectJudge.getTaskId());
    			task.setProgressStatus("1");
    			subjectTaskMapper.updateSubjectTask(task);
    		}    		
    	}
        return 1;
    }

    /**
     * 批量删除课题评审
     * 
     * @param ids 需要删除的课题评审ID
     * @return 结果
     */
    @Override
    public int deleteSubjectJudgeByIds(Long[] ids)
    {
        return subjectJudgeMapper.deleteSubjectJudgeByIds(ids);
    }

    /**
     * 删除课题评审信息
     * 
     * @param id 课题评审ID
     * @return 结果
     */
    @Override
    public int deleteSubjectJudgeById(Long id)
    {
        return subjectJudgeMapper.deleteSubjectJudgeById(id);
    }
}
